Escpaing a Promise in JavaScript
Whenever you use a Promise in JavaScript your code will be enclosed/jailed by the Promse, and all errors will be captured (maybe silently) by that promise. The promise call might reside in some library or module several layers deep.
Here's some code to show the problem:
In function foo() there is an error: Variable oops is not declared. The code that use the promise will however swallow that error.
By using setTimeout we can get out from the context of the promise, so we can see errors again.
Depromisify
With this knowelage we can now create a function that turns a Promise into a function.
(I have not tested the above function so there might be edge cases)
We can do most things in JavaScript using just functions, no need to complicate the code with Promises.
Async functions takes a while to master, but Promises are still async functions - only more complicated.
Written by Johan Zetterberg December 12th, 2020.